//
//  OrderInfoViewController.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 18/3/2.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "OrderInfoViewController.h"
#import "UIImageView+oldImageView.h"
#import "oldOrderBottomButtonView.h"
#import "OldUploadGroupPhotoViewController.h"
#import "NinaSelectionView.h"
#import "OldMapView.h"
#import "oldOrderBottomButtonView_ConfirmStep.h"
#import "oldOrderBottomButtonView_EndStep.h"
#import "oldOrderEndInfoViewController.h"
#import "oldTopGroupPhotoVIew.h"
#import "oldVoiceView.h"
#import "oldMapViewController.h"
#import "oldOrderBottomButtonView_FinishedStep.h"
#import "oldOrderBottomButtonView_PayedStep.h"
#import "oldOrderBottomButtonView_CancelStep.h"
#import "oldOrderPayViewController.h"
#import "oldMessageManager.h"
#import "oldMessageAlertView.h"
#import "oldVideoView.h"
#import "oldVideoFullScreenViewController.h"

#define default_topview_height 150.f
#define default_totalview_height 913.f
#define Lable_tag 500
#define VideoHeight 300

@interface OrderInfoViewController ()<oldOrderBottomButtonViewDelegate, NinaSelectionDelegate, oldOrderBottomButtonViewCofirmStepDelegate, oldOrderBottomButtonViewEndStepDelegate, OldMapViewDelegate, oldOrderBottomButtonView_PayedStepDelegate, oldOrderBottomButtonViewCancelStepDelegate, oldMessageDelegate, oldVideoViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *top_view;
@property (weak, nonatomic) IBOutlet UIImageView *infoView_avater;
@property (weak, nonatomic) IBOutlet UILabel *infoView_label_name;
@property (weak, nonatomic) IBOutlet UILabel *infoView_label_age;
@property (weak, nonatomic) IBOutlet UIButton *infoView_btn_phone;
@property (weak, nonatomic) IBOutlet UILabel *severView_label_type;
@property (weak, nonatomic) IBOutlet UIView *severView_voiceView;
@property (weak, nonatomic) IBOutlet UIButton *healthView_btn_more;
@property (weak, nonatomic) IBOutlet UIView *health_view;
@property (weak, nonatomic) IBOutlet UIView *remark_view;
@property (weak, nonatomic) IBOutlet UIButton *remarkView_btn_more;
@property (weak, nonatomic) IBOutlet UIImageView *familyView_img_avatar;
@property (weak, nonatomic) IBOutlet UILabel *familyView_label_name;
@property (weak, nonatomic) IBOutlet UILabel *familyView_label_relation;
@property (weak, nonatomic) IBOutlet UIButton *familyView_btn_call;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *healthView_height;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *total_height;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topView_height;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *remarkView_height;
@property (weak, nonatomic) IBOutlet UIView *clientView;
@property (nonatomic, strong) NinaSelectionView *ninaSelectionView;
@property (nonatomic, strong)OldMapView *mapView;
@property (nonatomic, strong)oldTopGroupPhotoVIew *groupPhotoView;
@property (nonatomic, assign)BOOL ifLookMore;
@property (nonatomic, assign)BOOL ifRemarkMore;
@property (nonatomic, assign)CGFloat healthLabelHeight;
@property (nonatomic, assign)CGFloat remarkLabelHeight;
@property (nonatomic, strong)oldVoiceView *voiceView;
@property (weak, nonatomic) IBOutlet UILabel *label_price;
@property (weak, nonatomic) IBOutlet UIButton *star1;
@property (weak, nonatomic) IBOutlet UIButton *star2;
@property (weak, nonatomic) IBOutlet UIButton *star3;
@property (weak, nonatomic) IBOutlet UIButton *star4;
@property (weak, nonatomic) IBOutlet UIButton *star5;
@property (weak, nonatomic) IBOutlet UILabel *label_authentication;
@property (weak, nonatomic) IBOutlet UILabel *label_orderCount;
@property (weak, nonatomic) IBOutlet UILabel *label_introducation;
@property (weak, nonatomic) IBOutlet UIButton *btn_endVideo;
@property (weak, nonatomic) IBOutlet UIButton *btn_healthTable;
@property (weak, nonatomic) IBOutlet UIView *videoView;

@property (nonatomic, strong)oldMessageManager *managerOrderStart;
@property (nonatomic, strong)oldMessageManager *managerOrderCancel;
@property (nonatomic, strong)oldMessageManager *managerOrderPay;
@end

@implementation OrderInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_voiceView stop];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self initTopView];
    [self initBottomView];
}

#pragma mark - LazyLoad
- (NinaSelectionView *)ninaSelectionView {
    if (!_ninaSelectionView) {
        _ninaSelectionView = [[NinaSelectionView alloc] initWithTitles:[self reasonsArray] PopDirection:NinaPopFromAboveToTop];
        _ninaSelectionView.ninaSelectionDelegate = self;
        _ninaSelectionView.defaultSelected = 0;
        _ninaSelectionView.shadowEffect = YES;
        _ninaSelectionView.shadowAlpha = 0.5;
        
        [self.view addSubview:_ninaSelectionView];
    }
    
    return _ninaSelectionView;
}

- (OldMapView *)mapView
{
    if (!_mapView) {
        _mapView = [OldMapView new];
        _mapView.frame = _top_view.bounds;
        _mapView.delegate = self;
    }
    
    return _mapView;
}

- (oldTopGroupPhotoVIew *)groupPhotoView
{
    if (!_groupPhotoView) {
        _groupPhotoView = [[[NSBundle mainBundle] loadNibNamed:@"oldTopGroupPhotoVIew"
                                                         owner:self
                                                       options:nil] objectAtIndex:0];
        [_groupPhotoView initSeverTimer:_orderInfo.startTime];
        [_groupPhotoView initWithPhotoUrl:_orderInfo.photoUrl];
    }
    
    return _groupPhotoView;
}

- (oldVoiceView *)voiceView
{
    if (!_voiceView) {
        _voiceView = [[[NSBundle mainBundle] loadNibNamed:@"oldVoiceView" owner:self options:nil] objectAtIndex:0];
        _voiceView.frame = CGRectMake(0, 0, 160, 46);
    }
    
    return _voiceView;
}

#pragma mark - TitlesArray
- (NSArray *)reasonsArray {
    return @[
             @"订单类型有误",
             @"订单描述不符",
             @"订单信息有误",
             @"护理距离太远",
             @"个人原因",
             @"其他原因"
             ];
}

#pragma mark - NinaSelectionDelegate
- (void)selectNinaAction:(UIButton *)button {
    [self cancelOrder:button.titleLabel.text];
    [self.ninaSelectionView showOrDismissNinaViewWithDuration:0.3];
}


#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"订单详情";
    
    [self initOldInfo];
//    [self initTopView];
    [self initSeverInfo];
    [self initPriceInfo];
    [self initRemarkInfo];
    [self initHealthInfo];
    [self initFamilyInfo];
    [self initVideoAndHealthTableInfo];
    [self initVideoView];
//    [self initBottomView];
    [self initMessage];
}


#pragma mark - initTopView
- (void)initTopView
{
    if ([_orderInfo.status integerValue] == New_Order || [_orderInfo.status integerValue] == Already_Accept) {
        [self initMapView];
    }
    else if (([_orderInfo.status integerValue] == Order_Begin) || ([_orderInfo.status integerValue] == Order_End) || ([_orderInfo.status integerValue] == Order_Payed))
    {
        [self initGroupPhotoView];
    }
}

- (void)initMapView
{
    if ([_groupPhotoView superview]) {
        [_groupPhotoView removeTopGroupPhotoView];
    }
    
    if (![self.mapView superview]) {
        [_top_view addSubview:self.mapView];
        _topView_height.constant = default_topview_height;
        _total_height.constant = default_totalview_height;
        _ifLookMore = NO;
    }
}

- (void)initGroupPhotoView
{
    if ([self.mapView superview]) {
        [self.mapView removeFromSuperview];
    }
    
    if (![self.groupPhotoView superview]) {
        _topView_height.constant = SCREEN_WIDTH/2;
        _total_height.constant = _total_height.constant - default_topview_height + SCREEN_WIDTH/2;
        [_top_view layoutIfNeeded];
        
        self.groupPhotoView.frame = _top_view.bounds;
        [_top_view addSubview:self.groupPhotoView];
    }
    
    if ([_orderInfo.status integerValue] != Order_Begin) {
        [_groupPhotoView initSeverTimer:nil];
    }
}

#pragma mark - initOldInfo
- (void)initOldInfo
{
    _infoView_avater.layer.cornerRadius = 30.f;
    
    [_infoView_avater showHeadImg:_orderInfo.serverInfo.avatar];
    _infoView_label_name.text = [NSString stringWithFormat:@"服务人员：%@", _orderInfo.serverInfo.uname];
    _infoView_label_age.text = [NSString stringWithFormat:@"%@岁", _orderInfo.serverInfo.age];
    _label_introducation.text = _orderInfo.serverInfo.addition.introduce;
    _label_orderCount.text = [NSString stringWithFormat:@"已服务%@单", _orderInfo.serverInfo.addition.orderCount];
    [self starsNumber:[_orderInfo.serverInfo.addition.userRate integerValue]];
    [_infoView_btn_phone addTarget:self action:@selector(btn_call_tapped:) forControlEvents:UIControlEventTouchDown];
}

- (void)starsNumber:(NSInteger)rate
{
    switch (rate) {
        case 0:
            [_star1 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star2 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star3 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star4 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star5 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            break;
        case 1:
            [_star1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star2 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star3 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star4 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star5 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            break;
        case 2:
            [_star1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star2 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star3 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star4 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star5 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            break;
        case 3:
            [_star1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star2 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star3 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star4 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            [_star5 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            break;
        case 4:
            [_star1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star2 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star3 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star4 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star5 setImage:[UIImage imageNamed:@"订单评价星星-空"] forState:UIControlStateNormal];
            break;
        default:
            [_star1 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star2 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star3 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star4 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            [_star5 setImage:[UIImage imageNamed:@"订单评价星星-满"] forState:UIControlStateNormal];
            break;
    }
}

- (void)btn_call_tapped:(id)sender
{
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",_orderInfo.serverInfo.mobile];
    // NSLog(@"str======%@",str);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
}

- (void)initSeverInfo
{
    _severView_label_type.text = [NSString stringWithFormat:@"服务分类： %@",  _orderInfo.serveCategory.cname];
    if (_orderInfo.voiceUrl && ![_orderInfo.voiceUrl isEqualToString:@""]) {
        [self initVoiceView];
    }
}

- (void)initPriceInfo
{
    _label_price.text = [NSString stringWithFormat:@"¥%@(约%ld分钟)",  _orderInfo.serveCategory.price, (long)[_orderInfo getTimeCostByMinutes]];
}

- (void)initHealthInfo
{
    [_healthView_btn_more addTarget:self action:@selector(look_more:) forControlEvents:UIControlEventTouchDown];
}

- (void)initRemarkInfo
{
    [_remarkView_btn_more addTarget:self action:@selector(remark_more:) forControlEvents:UIControlEventTouchDown];
}

- (void)initFamilyInfo
{
    if ([_orderInfo.clientInfo.role integerValue] == Role_Old) {
        _clientView.hidden = YES;
        return;
    }
    
    _familyView_img_avatar.layer.cornerRadius = 30.f;
    [_familyView_img_avatar showHeadImg:_orderInfo.clientInfo.avatar];
    _familyView_label_name.text = [NSString stringWithFormat:@"家属：%@", _orderInfo.clientInfo.uname];
    _familyView_label_relation.text = [NSString stringWithFormat:@"亲属关系：%@", _orderInfo.relation];
}

- (void)initVideoAndHealthTableInfo
{
    [_btn_endVideo addTarget:self action:@selector(btn_endVideo_tapped:) forControlEvents:UIControlEventTouchDown];
    [_btn_healthTable addTarget:self action:@selector(btn_healthTable_tapped:) forControlEvents:UIControlEventTouchDown];
    if(([_orderInfo.status integerValue] == Order_End) || ([_orderInfo.status integerValue] == Order_Payed))
    {
        _btn_healthTable.hidden = NO;
        _btn_endVideo.hidden = NO;
    }
    else
    {
        _btn_healthTable.hidden = YES;
        _btn_endVideo.hidden = YES;
    }
}

- (void)initVideoView
{
    if (_orderInfo.familyVideoUrl && ![_orderInfo.familyVideoUrl isEqualToString:@""]) {
        _total_height.constant += VideoHeight;
    }
    
    oldVideoView *view = [[oldVideoView alloc] initByType:VideoWeb url:_orderInfo.familyVideoUrl fillType:VideoFill];
    view.delegate = self;
    [_videoView addSubview:view];
}

- (void)initBottomView
{
    switch ([_orderInfo.status integerValue]) {
        case Order_End:
        {
            [self initPayStep];
        }
            break;
        case Order_Payed:
        {
            [self initFinishStep];
        }
            break;
        default:
        {
            [self initCancelStep];
        }
            break;
    }
}

- (void)bottonRemoveSubView
{
    for (UIView *subview in _bottomView.subviews) {
        if ([subview isKindOfClass:[oldOrderBottomButtonView class]] ||
            [subview isKindOfClass:[oldOrderBottomButtonView_ConfirmStep class]] ||
            [subview isKindOfClass:[oldOrderBottomButtonView_EndStep class]] ||
            [subview isKindOfClass:[oldOrderBottomButtonView_FinishedStep class]] ||
            [subview isKindOfClass:[oldOrderBottomButtonView_PayedStep class]] ||
            [subview isKindOfClass:[oldOrderBottomButtonView_CancelStep class]]) {
            [subview removeFromSuperview];
        }
    }
}

- (void)initCofirmStep
{
    [self bottonRemoveSubView];
    
    oldOrderBottomButtonView_ConfirmStep *view = [[[NSBundle mainBundle] loadNibNamed:@"oldOrderBottomButtonView_ConfirmStep" owner:self options:nil] objectAtIndex:0];
    view.delegate = self;
    view.frame = _bottomView.bounds;
    
    [_bottomView addSubview:view];
}

- (void)initStartSetp
{
    [self bottonRemoveSubView];
    
    oldOrderBottomButtonView *view = [[[NSBundle mainBundle] loadNibNamed:@"oldOrderBottomButtonView" owner:self options:nil] objectAtIndex:0];
    view.delegate = self;
    view.frame = _bottomView.bounds;
    
    [_bottomView addSubview:view];
}

- (void)initEndStep
{
    [self bottonRemoveSubView];
    
    oldOrderBottomButtonView_EndStep *view = [[[NSBundle mainBundle] loadNibNamed:@"oldOrderBottomButtonView_EndStep" owner:self options:nil] objectAtIndex:0];
    view.delegate = self;
    view.frame = _bottomView.bounds;
    
    [_bottomView addSubview:view];
}

- (void)initCancelStep
{
    [self bottonRemoveSubView];
    
    oldOrderBottomButtonView_CancelStep *view = [[[NSBundle mainBundle] loadNibNamed:@"oldOrderBottomButtonView_CancelStep" owner:self options:nil] objectAtIndex:0];
    view.delegate = self;
    view.frame = _bottomView.bounds;
    
    [_bottomView addSubview:view];
}

- (void)initPayStep
{
    [self bottonRemoveSubView];
    
    oldOrderBottomButtonView_PayedStep *view = [[[NSBundle mainBundle] loadNibNamed:@"oldOrderBottomButtonView_PayedStep" owner:self options:nil] objectAtIndex:0];
    view.delegate = self;
    view.frame = _bottomView.bounds;
    
    [_bottomView addSubview:view];
}

- (void)initFinishStep
{
    [self bottonRemoveSubView];
    
    oldOrderBottomButtonView_FinishedStep *view = [[[NSBundle mainBundle] loadNibNamed:@"oldOrderBottomButtonView_FinishedStep" owner:self options:nil] objectAtIndex:0];
    view.frame = _bottomView.bounds;
    
    if ([_orderInfo.status integerValue] == Order_End)
    {
        [view initFinishStepByMoney:_orderInfo.totalAmount ifPay:NO];
    }
    else
    {
        [view initFinishStepByMoney:_orderInfo.totalAmount ifPay:YES];
    }
    
    [_bottomView addSubview:view];
}

#pragma mark - initData
- (void)initData
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_OLD_GetOrderDetail params:@{@"oid" : _orderInfo.oid} success:^(id data) {
        weakSelf.orderInfo = [[oldOrderInfo alloc] initWithDictionary:data];
        [weakSelf initTopView];
        [weakSelf initBottomView];
    } error:^(NSString *error) {
        
    }];
}

#pragma mark - topViewDelegate
- (void)gotoDetailMapView
{
    oldMapViewController *vc = [oldMapViewController new];
    vc.lat = _orderInfo.lat;
    vc.lng = _orderInfo.lng;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - healthFuc
- (void)look_more:(id)sender
{
    _ifLookMore = !_ifLookMore;
    
    if (_ifLookMore) {
        [self initHealthHeight];
    }
    else
    {
        [self pickup:_health_view];
        _healthView_btn_more.titleLabel.text = @"查看详情";
        [_healthView_btn_more setTitle:@"查看详情" forState:UIControlStateNormal];
        _total_height.constant -= _healthLabelHeight;
        _healthView_height.constant -= _healthLabelHeight;
    }
}

- (void)initHealthHeight
{
    _healthLabelHeight = [self initHeightByType:_health_view height:_healthLabelHeight params:_orderInfo.attendeeInfo.addition.ps];
    _healthView_btn_more.titleLabel.text = @"收起";
    [_healthView_btn_more setTitle:@"收起" forState:UIControlStateNormal];
    _total_height.constant += _healthLabelHeight;
    _healthView_height.constant += _healthLabelHeight;
}


- (void)pickup:(UIView *)superView
{
    for(UIView *view in superView.subviews)
    {
        if ([view isKindOfClass:[UILabel class]]) {
            if (view.tag == Lable_tag) {
                [view removeFromSuperview];
                break;
            }
        }
    }
}

- (CGFloat)initHeightByType:(UIView *)superView height:(CGFloat)height params:(NSString *)params
{
    UILabel *label = [[UILabel alloc] init];
    label.tag = Lable_tag;
    [label setFont:[UIFont systemFontOfSize:14.f]];
    label.textColor = OLD_BLACK;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.numberOfLines = 0;
    
    label.text = [params length] ? params : @"无";
    
    if (height == 0) {
        CGSize s_label = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH - 30 , FLT_MAX)];
        height = s_label.height;
    }
    
    label.frame = CGRectMake(15, 42, SCREEN_WIDTH - 20, height);
    
    [superView addSubview:label];
    
    return height;
}

#pragma mark - remark
- (void)remark_more:(id)sender
{
    _ifRemarkMore = !_ifRemarkMore;
    
    if (_ifRemarkMore) {
        [self initRemarkHeight];
        _remarkView_btn_more.titleLabel.text = @"收起";
        [_remarkView_btn_more setTitle:@"收起" forState:UIControlStateNormal];
    }
    else
    {
        [self pickup:_remark_view];
        _remarkView_btn_more.titleLabel.text = @"查看详情";
        [_remarkView_btn_more setTitle:@"查看详情" forState:UIControlStateNormal];
        _total_height.constant -= _remarkLabelHeight;
        _remarkView_height.constant -= _remarkLabelHeight;
    }
}

- (void)initRemarkHeight
{
    _remarkLabelHeight = [self initHeightByType:_remark_view height:_remarkLabelHeight params:_orderInfo.remark];
    _remarkView_btn_more.titleLabel.text = @"收起";
    [_remarkView_btn_more setTitle:@"收起" forState:UIControlStateNormal];
    _total_height.constant += _remarkLabelHeight;
    _remarkView_height.constant += _remarkLabelHeight;
}

#pragma mark - severInfo
- (void)initVoiceView
{
    [_severView_voiceView addSubview:self.voiceView];
    [self.voiceView beginPlay:_orderInfo.voiceUrl];
}

#pragma mark - videoAndHealthTableInfo
- (void)btn_endVideo_tapped:(id)sender
{
    
}

- (void)btn_healthTable_tapped:(id)sender
{
    
}

#pragma mark - videoDelegate
- (void)touchedFullScreen:(NSString *)url type:(oldVideoType)type
{
    oldVideoFullScreenViewController *vc = [oldVideoFullScreenViewController new];
    vc.url = url;
    vc.type = type;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - bottomDelegate
- (void)btnOrderBegin
{
    OldUploadGroupPhotoViewController *vc = [[OldUploadGroupPhotoViewController alloc] init];
    vc.orderInfo = _orderInfo;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)btnOrderCofirm
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否确认当前订单?" message:@"确认订单后如果再取消会影响评分" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *chooseReason = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self orderBegin];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alert addAction:chooseReason];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)btnOrderCofirmStepCancel
{
    [self orderCancel];
}

- (void)btnOrderCancel
{
    [self orderCancel];
}

- (void)btnOrderEnd
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定已完成当前订单?" message:@"如果未完成服务被投诉后将会进行严格处理" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *chooseReason = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self endThisOrder];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alert addAction:chooseReason];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)orderBegin
{
    WeakSelf
    
    [oldHub waiting:@"确认订单中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_ConfirmOrder params:@{ @"oid" : _orderInfo.oid } success:^(id data) {
        [oldHub hideHUD];
        [oldHub warningUser:@"已成功接单！"];
        weakSelf.orderInfo = [[oldOrderInfo alloc] initWithDictionary:data];
        [weakSelf initStartSetp];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)orderCancel
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定要取消当前订单?（会影响评分）" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *chooseReason = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self chooseReason];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alert addAction:chooseReason];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)btnOrderPay
{
    oldOrderPayViewController *vc = [oldOrderPayViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    vc.orderInfo = _orderInfo;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)btnOldCancel
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定要取消当前订单?" message:@"匹配下一个工作人员需要较长时间" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *chooseReason = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self chooseReason];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alert addAction:chooseReason];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - endOrder
- (void)endThisOrder
{
//    WeakSelf
    
//    [oldHub waiting:@"订单提交中.."];
//    
//    [OLDNetWork httpRequestWithPostPut:URL_CloseOrder params:@{ @"oid" : _orderInfo.oid } success:^(id data) {
//        [oldHub hideHUD];
//        oldOrderEndInfoViewController *vc = [[oldOrderEndInfoViewController alloc] init];
//        vc.orderInfo = [[oldOrderInfo alloc] initWithDictionary:data];
//        UINavigationController *nvi = [[UINavigationController alloc]initWithRootViewController:vc];
//        
//        [weakSelf presentViewController:nvi animated:YES completion:^{
//            if ([vc isKindOfClass:[oldSupportFirstOrderListViewController class]]) {
//                oldSupportFirstOrderListViewController *oldVc = (oldSupportFirstOrderListViewController *)vc;
//                oldVc.current_order = weakSelf.orderInfo;
//            }
//            [self.navigationController popViewControllerAnimated:NO];
//        }];
//    } error:^(NSString *error) {
//        [oldHub warningUser:error];
//    }];
}

#pragma mark - cancelOrder

- (void)chooseReason
{
    [self.ninaSelectionView showOrDismissNinaViewWithDuration:0.5];
}

- (void)cancelOrder:(NSString *)reason
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:_orderInfo.oid, @"oid", reason, @"reason", nil];
    
    WeakSelf
    
    [oldHub waiting:@"订单取消中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_OLD_CancelOrder params:params success:^(NSDictionary *dic) {
        [oldHub warningUserAfterJump:@"订单已成功取消" time:1.0];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

#pragma mark - message
- (void)initMessage
{
    _managerOrderStart = [[oldMessageManager alloc] initWithType:orderStart];
    _managerOrderStart.delegate = self;
    [_managerOrderStart registerForNotifications];
    
    _managerOrderCancel = [[oldMessageManager alloc] initWithType:orderCancel];
    _managerOrderCancel.delegate = self;
    [_managerOrderCancel registerForNotifications];
    
    _managerOrderPay = [[oldMessageManager alloc] initWithType:orderEnd];
    _managerOrderPay.delegate = self;
    [_managerOrderPay registerForNotifications];
}

- (void)receiveMessage:(id)message type:(oldMessageType)type
{
    switch (type) {
        case orderCancel:
        {
            [[oldMessageAlertView shareInstance] showLabelWithString:@"订单已被取消"];
            [self.navigationController popViewControllerAnimated:YES];
        }
            break;
        case orderStart:
        {
            [self initData];
            [[oldMessageAlertView shareInstance] showLabelWithString:@"服务人员已上传合照"];
        }
            break;
        case orderEnd:
        {
            [self initData];
            [[oldMessageAlertView shareInstance] showLabelWithString:@"订单已完成，请及时支付"];
        }
            break;
        default:
            break;
    }
}

@end
